package com.rmfyzxfw.caseprocess.controller;

import com.rmfyzxfw.caseprocess.model.converter.DynamicDataSourceConverter;
import com.rmfyzxfw.caseprocess.model.po.DynamicDataSource;
import com.rmfyzxfw.caseprocess.model.vo.ColumnInfoVO;
import com.rmfyzxfw.caseprocess.model.vo.DynamicDataSourceVO;
import com.rmfyzxfw.caseprocess.model.vo.TableInfoVO;
import com.rmfyzxfw.caseprocess.service.DynamicDataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("api/v3/dataSource")
@RequiredArgsConstructor
@Api(tags = "动态数据源")
public class DynamicDataSourceController {
    private final DynamicDataSourceService dynamicDataSourceService;
    private final DynamicDataSourceConverter dynamicDataSourceConverter;

    @GetMapping("dataSourceList")
    @ApiOperation(value = "获取所有数据源")
    public List<DynamicDataSourceVO>  dataSourceList() {
        List<DynamicDataSource> dynamicDataSources = dynamicDataSourceService.list();
        return dynamicDataSourceConverter.dos2vos(dynamicDataSources);
    }

    @GetMapping("getTablesByDataSource/{dynamicDataSourceId}")
    @ApiOperation(value = "根据数据源获取表信息")
    public List<TableInfoVO> getTables(@PathVariable String dynamicDataSourceId) {
        return dynamicDataSourceService.getTables(dynamicDataSourceId);
    }

    @GetMapping("getTablesByDataSource/{dynamicDataSourceId}/{tableName}")
    @ApiOperation(value = "根据数据源和表名获取列信息")
    public List<ColumnInfoVO>  getColumnsByDataSource(@PathVariable String dynamicDataSourceId, @PathVariable String tableName) {
        return dynamicDataSourceService.getColumns(dynamicDataSourceId, tableName);
    }
}
